package com.csiec.clock.model;

import android.util.Log;

import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.callback.UserInfoCallBack;
import com.csiec.clock.utils.Constant;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UserInfoModel {

    private final String www = Constant.www;
    private final String editStudent = "/student/edit";
    private final String infoStudent = "/student/iclocklist";

    //修改资料的网络请求
    public void edit(final UserInfoCallBack callBack) {
        Log.d("UserInfoModel", "edit: " + UserInfo.getUserInfo().getStudentNumber());
        Log.d("UserInfoModel", "edit: " + UserInfo.getUserInfo().getPosition());
        Log.d("UserInfoModel", "edit: " + UserInfo.getUserInfo().getAddress());
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("studentNumber", UserInfo.getUserInfo().getStudentNumber())
                        .add("base64", UserInfo.getUserInfo().getBase64())
                        .add("position", UserInfo.getUserInfo().getPosition())
                        .add("address", UserInfo.getUserInfo().getAddress())
                        .build();
                Request request = new Request.Builder()
                        .url(www + editStudent)
                        .addHeader("token", UserInfo.getUserInfo().getToken())
                        .post(body)
                        .build();//创建一个Request对象
                Response response = client.newCall(request).execute();//发送请求获取返回数据
                String responseData = response.body().string();//处理返回的数据
                Log.d("UserInfoViewModel", "run: " + responseData);
                callBack.onSuccess(responseData);
            } catch (Exception e) {
                callBack.onFailed();
                e.printStackTrace();
            }
        }).start();

    }
}
